package com.bawei.module_home.ui.vip

import android.graphics.Color
import android.os.Build
import android.util.Log
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityVipBinding
import com.bawei.module_home.http.HomeServiceViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Locale

@AndroidEntryPoint
@Route(path = Router.VIP_PAGE)
class VipActivity : BaseActivity<BaseViewModel, ActivityVipBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_vip
    private val mViewModel: HomeServiceViewModel by viewModels()

    override fun initView() {
        super.initView()

        mBinding.vipBack.setOnClickListener {
            finish()
        }
        mBinding.vipTvName.setOnClickListener {
            if (!mBinding.vipTvName.text.equals("未登录")){
               return@setOnClickListener
            }
            ARouter.getInstance().build(Router.LOGIN_PAGE).navigation()
        }


        mBinding.vipBtnUpdate.setOnClickListener {
            if (mBinding.vipTvName.text.equals("未登录")){
                ARouter.getInstance().build(Router.LOGIN_PAGE).navigation()
                return@setOnClickListener
            }
            else if (mBinding.vipTvName.text.equals("黄金会员")){
                mViewModel.put_upgrade(AppMMKV.userId, "铂金会员")
                mBinding.vipVip1.setTextColor(Color.parseColor("#4B5360"))
                mBinding.vipVip1.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                mBinding.vipVip2.background=resources.getDrawable(R.drawable.vip_rounded_corners)
                mBinding.vipVip3.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                mViewModel.getVip(AppMMKV.userId)

            }
            else if (mBinding.vipTvName.text.equals("铂金会员")){
                mViewModel.put_upgrade(AppMMKV.userId, "钻石会员")
                mBinding.vipVip2.setTextColor(Color.parseColor("#4B5360"))
                mBinding.vipVip1.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                mBinding.vipVip2.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                mBinding.vipVip3.background=resources.getDrawable(R.drawable.vip_rounded_corners)
                mViewModel.getVip(AppMMKV.userId)
            }
            if (mBinding.vipTvName.text.equals("钻石会员")){
                ToastUtils.showLong(this,"已经是最高会员")
                return@setOnClickListener
            }




        }

    }






    override fun initData() {
        mViewModel.getVip(AppMMKV.userId)
    }


    override fun initListener() {


        mViewModel.updateVipState.observeWithLifecycle(this){
            when(it){
                //
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this,"升级失败")
                }
                Loading -> {

                }
                Success -> {
                    ToastUtils.showLong(this,"升级成功")
                    mViewModel.getVip(AppMMKV.userId)
                }
            }
        }

        mViewModel.vipState.observeWithLifecycle(this){
            when(it){
                //
                Empty -> {}
                is Failed -> {
                    Log.d("vip1", "initListener: "+mViewModel.vipUser.value)
                    mBinding.vipTvName.setText("未登录")
                    mBinding.vipBtnUpdate.setText("充值")
                    mBinding.vipBtnUpdate.setTextColor(Color.BLACK)
                    mBinding.vipBtnUpdate.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                    mBinding.vipTvTime.setText("")
                    mBinding.vipVip1.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                    mBinding.vipVip1.setTextColor(Color.BLACK)
                }
                Loading -> {

                }
                Success -> {
                    Log.d("vip", "initListener: "+mViewModel.vipUser.value)
                    mBinding.vipTvName.setText(mViewModel.vipUser.value?.membershipLevel)

                    if (mViewModel.vipUser.value?.membershipLevel.equals("黄金会员")){
                        mBinding.vipVip1.setTextColor(Color.WHITE)
                        mBinding.vipVip1.background=resources.getDrawable(R.drawable.vip_rounded_corners)
                    }else if (mViewModel.vipUser.value?.membershipLevel.equals("铂金会员")){
                        mBinding.vipVip2.setTextColor(Color.WHITE)
                        mBinding.vipVip2.background=resources.getDrawable(R.drawable.vip_rounded_corners)
                        mBinding.vipVip1.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                        mBinding.vipVip1.setTextColor(Color.BLACK)
                    }
                    else if (mViewModel.vipUser.value?.membershipLevel.equals("钻石会员")){
                        mBinding.vipVip3.setTextColor(Color.WHITE)
                        mBinding.vipVip3.background=resources.getDrawable(R.drawable.vip_rounded_corners)
                        mBinding.vipVip2.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                        mBinding.vipVip2.setTextColor(Color.BLACK)
                        mBinding.vipVip1.background=resources.getDrawable(R.drawable.vip_rounded_corners1)
                        mBinding.vipVip1.setTextColor(Color.BLACK)
                    }
                    mBinding.mViewModel=mViewModel

                    }


                }
            }
        }

    }




